1468K - The Robot - CodeForces Solution


brute force implementation *1600

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    path=[]
    s=input()
    x=0
    y=0
    for i in s:
        if i=="U":
            y+=1
        elif i=="D":
            y-=1
        elif i=="R":
            x+=1
        else :
            x-=1
        path+=[[x,y]]
    
    test=0
    for i in path:
        
        xx=i[0]
        yy=i[1]
        if not(xx==0 and yy==0):
            x=0
            y=0
            for i in s:
                if i=="U" and not (x==xx and y==yy-1):
                    y+=1
                elif i=="D" and not (x==xx and y==yy+1):
                    y-=1
                elif i=="R" and not (x==xx-1 and y==yy):
                    x+=1
                elif i=="L" and not (x==xx+1 and y==yy) :
                    x-=1
            if x==0 and y==0:
                test=1
                res=[xx,yy]
                break
    if test==0:
        print("0 0")
    else:
        print(*res)
            
        
	 	 	 	 	   	 		 			   	  	 			

C++ Code:

#include"bits/stdc++.h"

using namespace std;

typedef long long ll;

ll   a[1000005];

ll   b[1000005];

string s;

std::map<pair<ll,ll>,pair<ll,ll> >map1;

std::map<pair<ll,ll>,ll> vis;

int main()

{

	#ifndef ONLINE_JUDGE 

   	freopen("input.txt", "r", stdin); 

   	freopen("error.txt", "w", stderr); 

   	freopen("output.txt", "w", stdout); 

   	#endif

   	ll t;

	cin>>t;

	ll t1=t;

	ll k=1;

	ll ct=1;

	while(t--)

	{

		cin>>s;

		for(ll i=0;i<s.length();i++)

		{

			if(s[i]=='L')

			{

				a[i+1]=a[i]-1;

				b[i+1]=b[i];

			}

			else if(s[i]=='R')

			{

				a[i+1]=a[i]+1;

				b[i+1]=b[i];

			}

			else if(s[i]=='U')

			{

				a[i+1]=a[i];

				b[i+1]=b[i]+1;

			}

			else if(s[i]=='D')

			{

				a[i+1]=a[i];

				b[i+1]=b[i]-1;

			}

		}

		for(ll i=s.length();i>0;i--)

		{

			//cout<<a[i]<<" "<<b[i]<<'\n';

			std::map<pair<ll,ll>,pair<ll,ll> >::iterator it;

			if(s[i-1]=='L')

			{

				it=map1.find(make_pair(a[i]-1,b[i]));

				if(it!=map1.end())

					map1[make_pair(a[i],b[i])]=make_pair(map1.find(make_pair(a[i]-1,b[i]))->second.first+1,map1.find(make_pair(a[i]-1,b[i]))->second.second);

				else

					map1[make_pair(a[i],b[i])]=make_pair(1,0);

			}

			else if(s[i-1]=='R')

			{

				it=map1.find(make_pair(a[i]+1,b[i]));

				if(it!=map1.end())

					map1[make_pair(a[i],b[i])]=make_pair(map1.find(make_pair(a[i]+1,b[i]))->second.first-1,map1.find(make_pair(a[i]+1,b[i]))->second.second);

				else

					map1[make_pair(a[i],b[i])]=make_pair(-1,0);

			}

			else if(s[i-1]=='U')

			{

				it=map1.find(make_pair(a[i],b[i]+1));

				if(it!=map1.end())

					map1[make_pair(a[i],b[i])]=make_pair(map1.find(make_pair(a[i],b[i]+1))->second.first,map1.find(make_pair(a[i],b[i]+1))->second.second-1);

				else

					map1[make_pair(a[i],b[i])]=make_pair(0,-1);

			}

			else if(s[i-1]=='D')

			{

				it=map1.find(make_pair(a[i],b[i]-1));

				if(it!=map1.end())

					map1[make_pair(a[i],b[i])]=make_pair(map1.find(make_pair(a[i],b[i]-1))->second.first,map1.find(make_pair(a[i],b[i]-1))->second.second+1);

				else

					map1[make_pair(a[i],b[i])]=make_pair(0,+1);

			}

			vis[make_pair(a[i],b[i])]=0;

		}

		ll u=0;

		ll a1=a[s.length()];

		ll b1=b[s.length()];

		//cout<<a1<<" "<<b1<<"\n\n";

		for(int i=1;i<=s.length();i++)

		{

			//cout<<a[i]<<" "<<b[i]<<" ";

			//cout<<map1.find(make_pair(a[i],b[i]))->second.first<<" ";

			//cout<<map1.find(make_pair(a[i],b[i]))->second.second<<'\n';

			if(vis[make_pair(a[i],b[i])]!=1)

			{

				if(((a1+map1.find(make_pair(a[i],b[i]))->second.first)==0)&&((b1+map1.find(make_pair(a[i],b[i]))->second.second)==0))

				{

					

					cout<<a[i]<<" "<<b[i]<<"\n";

					u=1;

					break;

					

				}

				vis[make_pair(a[i],b[i])]=1;

			}

		}

		if(u==0)

		{

			cout<<"0 0\n";

		}

		map1.clear();

		vis.clear();

		for(int i=0;i<=s.length();i++)

		{

			a[i]=0;

			b[i]=0;

		}

	}

	cout<<'\n';

	return 0;

}


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship